/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filecopier.restore;

import java.io.File;
import java.io.IOException;
import filecopier.Singleton;
import filecopier.ActionCancelException;
import filecopier.script.Script;

/**
 *
 * @author david
 */
public abstract class AbstractRestoreWorker {

    protected Script restoreScript = null;
    protected boolean promptForRestore = false;

    public AbstractRestoreWorker(final Script restoreScript) {
        this.restoreScript = restoreScript;
        promptForRestore = Singleton.getInstance().getOptions().promptForRestore();
    }

    public void restoreFile(final File restoreFile, final File deleteFile) throws IOException, FileRestoreCancelException {
        if (!restoreFile.exists()) {
            throw new IOException("\"" + restoreFile + "\" cannot be renamed to \"" + deleteFile + "\" because \"" +
                    restoreFile + "\" does not exist.");
        }

        if (!deleteFile.exists()) {
            throw new IOException("\"" + restoreFile + "\" cannot be renamed to \"" + deleteFile + "\" because \"" +
                    deleteFile + "\" does not exist.");
        }

        if ((!promptForRestore) || (promptForRestore && proceed(restoreFile, deleteFile))) {
            File deleteTempFile;
            deleteTempFile = new File(deleteFile + ".delete");

            if (!deleteFile.renameTo(deleteTempFile)) {
                throw new IOException("\"" + deleteFile + "\" could not be renamed to \"" + deleteTempFile + "\"");
            }
            if (!restoreFile.renameTo(deleteFile)) {
                throw new IOException("\"" + restoreFile + "\" could not be renamed to \"" + deleteFile + "\"");
            }

            if (deleteTempFile.isFile()) {
                if (!deleteTempFile.delete()) {
                    throw new IOException("\"" + deleteTempFile + "\" could not be deleted. File must be deleted manually.");
                }
            } else {
                // File is a folder, so its contents must be deleted before deleting the folder.
                deleteFolder(deleteTempFile);
            }
        } else {
            // If we get here, promptForRestore was true and the user chose to not restore the file.
            throw new IOException("\"" + deleteFile + "\" was not renamed to \"" + deleteFile + "\" because user chose not to restore.");
        }
    }

    public void deleteFolder(final File folder) throws IOException {
        File[] files = folder.listFiles();
        for (File f : files) {
            if (f.isFile()) {
                if (!f.delete()) {
                    throw new IOException(f + " in " + folder + " could not be deleted. File must be deleted manually.");
                }
            } else {
                // File is a folder. Its contents must also be deleted before it can be deleted.
                deleteFolder(f);
            }
        }
        // Now, we can delete the folder.
        if (!folder.delete()) {
            throw new IOException(folder + " could not be deleted. File must be deleted manually.");
        }
    }

    public abstract void restoreFiles() throws ActionCancelException;

    protected abstract boolean proceed(final File restoreFile, final File deleteFile) throws FileRestoreCancelException;
}
